package com.ctf.soft.ms.controls.manage;

import com.ctf.soft.ms.dao.dto.manage.Pager;
import com.ctf.soft.ms.dao.model.manage.User;
import com.ctf.soft.ms.service.manage.IUserService;
import com.ctf.soft.ms.utils.AnnotationUtil;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Column;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 名称： UserController<br>
 * 描述： 系统用户控制器<br>
 * 最近修改时间：2015/7/31 11:23<br>
 * 修改说明：
 *
 * @author jiajl
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/main")
    public String doMain() {
        return "manage/user/main";
    }

    @RequestMapping("/tree")
    public String doTree() {
        return "manage/user/tree";
    }

    @RequestMapping("/view")
    public String doView() {
        return "manage/user/view";
    }

    @RequestMapping("/editor")
    public String doEditor() {
        return "manage/user/editor";
    }

    @RequestMapping("/SaveOrUpdate")
    @ResponseBody
    public String doSaveOrUpdate(@RequestBody User user,
                                 HttpServletRequest request) {
        String result = "faild";
        try {
            int c = -1;
            if (user.getId() == null || user.getId().trim().equals("")) {
                user.setId(ToolsUtil.getRandomForUUID());
                user.setDefaultOrg("");
                user.setPassword(ToolsUtil.encryptedPwd(user.getPassword()
                        .trim()));
                c = userService.insertSelective(user);
            } else {
                if (!ToolsUtil.isEmpty(user.getPassword())) {
                    user.setPassword(ToolsUtil.encryptedPwd(user.getPassword()
                            .trim()));
                }
                c = userService.updateByPrimaryKeySelective(user);
            }
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Delete")
    @ResponseBody
    public String doDelete(@RequestParam("id") String id,
                           HttpServletRequest request) {
        String result = "faild";
        try {
            int c = -1;
            User user = new User();
            user.setId(id);
            user.setStatus("D");
            c = userService.updateByPrimaryKeySelective(user);
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Data")
    @ResponseBody
    public User loadData(@RequestParam("id") String id,
                         HttpServletRequest request) {
        User user = null;
        try {
            user = userService.selectByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @RequestMapping("/List")
    @ResponseBody
    public List<User> loadList(HttpServletRequest request) {
        List<User> list = null;
        try {
            User params = new User();
            if (!ToolsUtil.isEmpty(request.getParameter("org"))) {
                params.setDefaultOrgCode(request.getParameter("org").toString().trim());
            }
            if (!ToolsUtil.isEmpty(request.getParameter("dept"))) {
                params.setDefaultDeptCode(request.getParameter("dept").toString().trim());
            }
            params.setStatus("Y");
            list = userService.selectByList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping("/PageList")
    @ResponseBody
    public Pager<User> loadPageList(@RequestBody ModelMap params,
                                    HttpServletRequest request) {
        Pager<User> pager = null;
        try {
            params.put("status", "D");
            String sidx = (String) params.get("sidx");
            String sord = (String) params.get("sord");
            params.put("sord", sord);
            params.put("sidx", AnnotationUtil.getColumnValue(User.class, Column.class, sidx, "name"));

            int totalRecord = userService.getPageCount(params);
            int page = Integer.valueOf(params.get("page").toString());
            int rows = Integer.valueOf(params.get("rows").toString());
            int totalPage = totalRecord % rows == 0 ? totalRecord / rows
                    : totalRecord / rows + 1;
            List<User> list = userService.loadPageList(params);
            if (list != null) {
                pager = new Pager<User>();
                pager.setCurrPage(page);
                pager.setTotalPage(totalPage);
                pager.setTotalRecord(totalRecord);
                pager.setList(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pager;
    }

}
